#include "Paddle.h"



Paddle::Paddle()
{
	m_score = 0;
	m_Size=Vec2f(20,100);
	m_Vel=Vec2f(0,0);
	LPDIRECT3DVERTEXBUFFER9 g_pVP = NULL; // Buffer to hold paddle vertices
}

void Paddle::CheckHit()
{

}
HRESULT Paddle::Load()
{
	if(FAILED(D3DXCreateTextureFromFile(TheGame::Instance()->m_pd3dDevice, L"Paddle.png", GetTexture())))
	{
		//If texture isn't in current folder, check parent folder
		if(FAILED(D3DXCreateTextureFromFile(TheGame::Instance()->m_pd3dDevice, L"..\\Paddle.png", GetTexture())))
		{
			MessageBox(0, L"Could not find texture", L"Texture error", 0);
			return E_FAIL;
		}
	}
	return S_OK;
}
HRESULT Paddle::Update()
{
	m_Pos+=m_Vel;
	if(m_Pos.Y()<(m_Size.Y()/2))
	{
		m_Pos.SetY(m_Size.Y()/2);
	}
	if(m_Pos.Y()>TheGame::Instance()->GetWinH()-(m_Size.Y()/2))
	{
		m_Pos.SetY(TheGame::Instance()->GetWinH()-(m_Size.Y()/2));
	}

	CUSTOMVERTEX paddle[] =
	{
		{ m_Pos.X()-(m_Size.X()/2),  m_Pos.Y()-(m_Size.Y()/2), 0.5f, 1.0f, 0, 1}, // x, y, z, rhw, tu, tv
		{ m_Pos.X()+(m_Size.X()/2), m_Pos.Y()-(m_Size.Y()/2), 0.5f, 1.0f, 1, 1},
		{  m_Pos.X()-(m_Size.X()/2), m_Pos.Y()+(m_Size.Y()/2), 0.5f, 1.0f, 0, 0},
		{  m_Pos.X()+(m_Size.X()/2), m_Pos.Y()+(m_Size.Y()/2), 0.5f, 1.0f, 1, 0},
	};

	//Update collision volume
	m_volume.left = (long)((-m_Size.X() / 2) + m_Pos.X());
	m_volume.top = (long)((-m_Size.Y() / 2) + m_Pos.Y());
	m_volume.right = (long)((m_Size.X() / 2) + m_Pos.X());
	m_volume.bottom = (long)((m_Size.Y() / 2) + m_Pos.Y());

	// Create the vertex buffer. Here we are allocating enough memory
	// (from the default pool) to hold all our 4 custom vertices. We also
	// specify the FVF, so the vertex buffer knows what data it contains.
	if( FAILED( TheGame::Instance()->m_pd3dDevice->CreateVertexBuffer( 4 * sizeof( CUSTOMVERTEX ),
		0, D3DFVF_CUSTOMVERTEX,
		D3DPOOL_DEFAULT, &m_pVB, NULL ) ) )
	{
		return E_FAIL;
	}

	// Now we fill the vertex buffer. To do this, we need to Lock() the VB to
	// gain access to the vertices. This mechanism is required becuase vertex
	// buffers may be in device memory.
	VOID* pVertP;
	if( FAILED( m_pVB->Lock( 0, sizeof( paddle ), ( void** )&pVertP, 0 ) ) )
		return E_FAIL;
	memcpy( pVertP, paddle, sizeof( paddle ) );
	m_pVB->Unlock();

	return S_OK;
}
void Paddle::Draw()
{
	//Draw Paddle
	TheGame::Instance()->m_pd3dDevice->SetTexture(0, m_Texture);
	TheGame::Instance()->m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
	TheGame::Instance()->m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
	TheGame::Instance()->m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
	TheGame::Instance()->m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE );

	TheGame::Instance()->m_pd3dDevice->SetStreamSource( 0, m_pVB, 0, sizeof( CUSTOMVERTEX ) );
	TheGame::Instance()->m_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
	TheGame::Instance()->m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );
}

void Paddle::SetScore(int score)
{
	m_score = score;
}

int Paddle::GetScore()
{
	return m_score;
}